//
//  ASImagesCellSupport.swift
//  SmoSky
//
//  Created by zhu xietong on 2018/5/21.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import Eelay
import AsyncDisplayKit
import Lantern
import Kingfisher
import AVFoundation


public class CommentImgKitDemo: ASScanSupportKit {
    
}
public class CommentCellDemo: ASImagesScanCell<CommentImgKitDemo> {
    
    override public func addLayoutRules() {
    
        super.addLayoutRules()
    }
    
    override public func mo_model_reload() {
        let list = [
            ["img":__images.one_str],
            ["img":__images.one_str],
            ["img":__images.one_str],
            ["img":__images.one_str],
            ["img":__images.one_str],
            ["img":__images.one_str],
            ]
        imgsView.mo.model = [
            "list":list
            ].mutable_dictionary
        
        var models = [EeImageResourceModel]()
        
        for one in list{
            let newm = EeImageResourceModel()
            newm.firstLevelUrl = one["img"]!
            newm.secondLevelUrl = one["img"]!
            newm.thirdLevelUrl = one["img"]!
            models.append(newm)
            
        }
        
    }
    
    override public func specGetter() -> _Spec {
        let spec:_Spec = .inset([], .zero, child: imgsView[Swidth])
        
        return spec
    }
}


open class ASScanSupportKit: JoTouchNode {
    
    
    public var imageNode = JoImageNode()
    
    open var itemSize:CGSize{
        get{
            return CGSize(width: 50, height: 50)
        }
    }
    
    open var space:(X:CGFloat,Y:CGFloat){
        get{
            return (8,8)
        }
    }
    
    
    open override func specGetter() -> _Spec {
        let size:CGSize = itemSize
        
        let inset:_Spec = .inset([], [4,0,4,8], child: imageNode["\(size.height)",size.width])
        return inset.addOver(button.Spec)
    }
    
    open override func addLayoutRules() {
        button.addTarget(self, action: #selector(buttonAction), forControlEvents: .touchUpInside)

    }
    
    public var imageURL:URL?{
        return URL(string: mo.model["img",""])
    }
    
    override open func mo_model_reload() {
        
        
        let img_url = mo.model["img",""]
        imageNode.img_url = img_url
        imageNode.view.backgroundColor = UIColor._tableBackground
    }
    
}


open class ASImagesScanCell<Kit:ASScanSupportKit>:ASSizeRangeCell,EeActionDelegate{
    
    public let imgsView = JoRuleNode<Kit>()
    

    
    open func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
        if let kit = onwer as? Kit
        {
          
            preview(item: kit)
            //            showImg(view: t)
        }
    }
    
    open override func addLayoutRules() {
        imgsView.delegate = self
        imgsView.space = 0
    }
    public typealias ItemKit = Kit

    
    open func preview(item: ItemKit) {
        
        let lantern = Lantern()
        let kits = imgsView.nodes
        lantern.numberOfItems = {
            return kits.count
        }
        lantern.cellClassAtIndex = { index in
            let kit = kits[index]
            let type = kit.mo.model["type",""]
            switch type {
            case "video":
                return VideoZoomCell.self
            default:
                return LanternImageCell.self
            }
        }
        lantern.reloadCellAtIndex = { context in
            LanternLog.high("reload cell!")
            let kit = kits[context.index]
            let type = kit.mo.model["type",""]

            switch type {
            case "video":
                if let cell = context.cell as? VideoZoomCell{
                    cell.imageView.image = kit.imageNode.image
                    if let src = kit.mo.model["fileURL"] as? URL{
                        cell.player.replaceCurrentItem(with: AVPlayerItem(url: src))
                    }
                }
            default:
                if let cell = context.cell as? LanternImageCell{
//                    cell.imageView.image = kit.imageNode.image
                    let placeholder = kit.imageNode.image
                    // 用Kingfisher加载
                    cell.imageView.kf.setImage(with: kit.imageURL, placeholder: placeholder)
                }
            }
            
        }
        lantern.cellWillAppear = { cell, index in
            let kit_ = kits[index]
            let type = kit_.mo.model["type",""]

            switch type {
            case "video":
                if let cell = cell as? VideoZoomCell{
                    cell.player.play()

                }
            default:
              break
            }

        }
        lantern.cellWillDisappear = { cell, index in
            let kit_ = kits[index]
            let type = kit_.mo.model["type",""]
            switch type {
            case "video":
                if let cell = cell as? VideoZoomCell{
                    cell.player.pause()
                }
            default:
              break
            }
        }
        lantern.transitionAnimator = LanternZoomAnimator(previousView: { index -> UIView? in
            let kit = kits[index]
            return kit.imageNode.imageView
        })
        lantern.pageIndex = max(item.nodeIndex, 0)
        lantern.show(method: .present(fromVC: mo.controller, embed: .none))

    }

        
}



